package com.sgb.capital.view.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.capital.R;
import com.sgb.capital.databinding.PledgeFragmentBinding;
import com.sgb.capital.model.DepositBean;
import com.sgb.capital.utils.Utils;
import com.sgb.capital.view.ui.activity.BillOrderDetailsActivity;
import com.sgb.capital.view.ui.adapter.PledgeAdapter;
import com.sgb.capital.viewmodel.DetailsModel;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

/**
 * 作者:张磊
 * 日期:2022/1/14 0014
 * 说明:保证金信息
 */
public class PledgeFragment extends AppLazyFragment {

    private PledgeFragmentBinding mBinding;
    private DetailsModel mModel;
    private PledgeAdapter mAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.pledge_fragment, container, false);
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        mModel = new ViewModelProvider(this).get(DetailsModel.class);
        mAdapter = new PledgeAdapter(null);
        mBinding.rvList.setLayoutManager(new GridLayoutManager(Utils.getContext(), 1));
        mBinding.rvList.setAdapter(mAdapter);
    }

    @Override
    public void initObserve() {
        mModel.mRecords.observe(this, data -> {
            if (data == null) {
                mBinding.errorView.setVisibility(View.VISIBLE);
                return;
            }
            mBinding.errorView.setVisibility(View.GONE);
            List<DepositBean>  depositBeans = new ArrayList<>();
            depositBeans.add(data);
            mAdapter.setNewInstance(depositBeans);
            mAdapter.notifyDataSetChanged();
        });
    }

    @Override
    protected void onLazyLoad() {
        BillOrderDetailsActivity activity = (BillOrderDetailsActivity) getActivity();
        mModel.getDetailByPayNo(activity, activity.mPayDetailBean.orderId);
    }
}